package v1

import (
	"com.cyt.common/library/response"
	"com.cyt.common/model"
	"fmt"
	"github.com/gogf/gf/frame/gmvc"
	"github.com/gogf/gf/net/ghttp"
)

// 控制器
type Controller struct {
	gmvc.Controller
}

// 初始化
func (c *Controller) Init(r *ghttp.Request) {
	c.Controller.Init(r)
}

// 成功返回
func (c *Controller) Success(msg string, data ...interface{}) {
	if len(data) == 1 {
		response.Result(c.Request, response.StatusOK, msg, data[0])
	}
	response.Result(c.Request, response.StatusOK, msg, data)
}

// 错误返回
func (c *Controller) Error(msg string, data ...interface{}) {
	response.Result(c.Request, -1, msg, data)
}

// 返回结果
func (c *Controller) Result(code int, msg string, data ...interface{}) {
	response.Result(c.Request, code, msg, data)
}

// 分页输出
func (c *Controller) Paginate(data *model.PageResult) {
	c.Success("成功", data)
}

// 验证
func (c *Controller) Valid(validator interface{}) {
	if err := c.Request.Parse(validator); err != nil {
		response.Result(c.Request, 422, err.Error())
	}
}

// 打印调试
func Dump(a ...interface{}) {
	fmt.Println(a...)
}
